package com.app.twth.ui.cyclecount.location;


import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityCyclecountLocationSubmitBinding;
import com.app.twth.ui.cyclecount.CycleCountViewModel;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @describe :
 */
public class CCLocationSubmitActivity extends BaseActivity<ActivityCyclecountLocationSubmitBinding, CycleCountViewModel> {
    private String TAG = getClass().getSimpleName();
    private int type = 0;
    CycleCountLocationQr cycleCountLocationQr;
    private CCLocationSubmitAdapter adapter;
    List<CycleCountLocationQr.SkuListDTO> skuListDTOS = new ArrayList<>();

    @Override
    protected void initListener() {
        viewModel.cycleCountLocationQrLiveData.observe(this, data -> {
            Intent intent = new Intent();
            intent.putExtra("data", data);
            setResult(RESULT_OK, intent);
            finish();
        });
        viewModel.skuBarcodesLiveData.observe(this, data -> {
            boolean isExist = false;
            for (CycleCountLocationQr.SkuListDTO skuListDTO : skuListDTOS) {
                if (skuListDTO.getSku().equals(data.getSku())) {
                    isExist = true;
                    skuListDTO.setQty(skuListDTO.getQty() + 1);
                }
            }
            if (!isExist) {
                CycleCountLocationQr.SkuListDTO skuListDTO = new CycleCountLocationQr.SkuListDTO();
                skuListDTO.setSku(data.getSku());
                skuListDTO.setQty(1);
                skuListDTOS.add(skuListDTO);
                adapter.setData(skuListDTOS);
            } else {
                adapter.notifyDataSetChanged();
            }
        });
        viewModel.submitLiveData.observe(this, data -> {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "CycleCount(Submit)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext(getString(R.string.submit));
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        cycleCountLocationQr = (CycleCountLocationQr) bundle.getSerializable("data");

        binding.tvLocationValue.setText(cycleCountLocationQr.getLocationCode());
        binding.tvIblpnValue.setText(cycleCountLocationQr.getIbLpn());
        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);

        adapter = new CCLocationSubmitAdapter(this, skuListDTOS, position -> {
            CycleCountLocationQr.SkuListDTO skuListDTO = skuListDTOS.get(position);
            int quantity = skuListDTO.getQty();
            quantity--;
            if (quantity < 1) {
                skuListDTOS.remove(position);
                adapter.setData(skuListDTOS);
            } else {
                skuListDTO.setQty(quantity);
                adapter.notifyDataSetChanged();
            }
        }, position -> {
            skuListDTOS.remove(position);
            adapter.setData(skuListDTOS);
        });
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
        layoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager2);
        binding.recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                bottomLayoutBinding.tvHint.setText("");
                binding.tvCode.setText("");
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
                bottomLayoutBinding.tvHint.setText("");
                if (skuListDTOS.size() == 0) {
                    bottomLayoutBinding.tvHint.setText("Please enter the sku or barcode");
                    return;
                }

                CycleCountLocationQr submitBean = new CycleCountLocationQr();
                submitBean.setLocationCode(cycleCountLocationQr.getLocationCode());
                submitBean.setIbLpn(cycleCountLocationQr.getIbLpn());
                submitBean.setSkuList(skuListDTOS);
                viewModel.cycleCountLocationLpnSubmit(submitBean);
                break;
        }
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                if ((!Utils.isEmpty(barcodeStr)) && (!judgeSku(barcodeStr))) {
                    viewModel.getInfoByBarcode(barcodeStr);
                }
            }
            return false;
        }
    };

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        bottomLayoutBinding.tvHint.setText("");
        binding.tvCode.setText(barcodeStr);
        if (!judgeSku(barcodeStr)) {
            viewModel.getInfoByBarcode(barcodeStr);
        }
    }

    public boolean judgeSku(String sku) {
        boolean isExist = false;
        for (CycleCountLocationQr.SkuListDTO skuListDTO : skuListDTOS) {
            if (skuListDTO.getSku().equals(sku)) {
                isExist = true;
                skuListDTO.setQty(skuListDTO.getQty() + 1);
            }
        }
        if (isExist) {
            adapter.notifyDataSetChanged();
        }
        return isExist;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
